﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace yafc.Activity
{
	class Task
	{
		private AutoResetEvent done = new AutoResetEvent(false);
		private WaitCallback callback;
		private Object arg = null;

		public Task(WaitCallback Callback)
		{
			callback = Callback;
		}
		public Task(WaitCallback Callback, Object o)
		{
			callback = Callback;
			arg = o;
		}
		public virtual void Start()
		{
			if (callback != null)
			{
				ThreadPool.QueueUserWorkItem(Callback, arg);
			}
		}
		public virtual void Wait()
		{
			done.WaitOne();
		}
		public virtual void Wait(int millisecondsTimeout)
		{
			done.WaitOne(millisecondsTimeout);
		}
		protected virtual void Callback(Object o)
		{
			callback(o);
			done.Set();
		}
	}
}
